// 封装 uni 弹窗
export const showModal = (options) => {
    const defaultOptions = {
        title: "提示",
        content: "",
        showCancel: true,
        cancelText: "取消",
        confirmText: "确认",
        success: null,
        fail: null,
        complete: null,
    };

    const mergedOptions = { ...defaultOptions, ...options };

    return new Promise((resolve, reject) => {
        uni.showModal({
            ...mergedOptions,
            success: (res) => {
                if (mergedOptions.success) {
                    mergedOptions.success(res);
                }
                if (res.confirm) {
                    resolve(res);
                } else if (res.cancel) {
                    reject(res);
                }
            },
            fail: (err) => {
                if (mergedOptions.fail) {
                    mergedOptions.fail(err);
                }
                reject(err);
            },
            complete: (res) => {
                if (mergedOptions.complete) {
                    mergedOptions.complete(res);
                }
            },
        });
    });
};

// 封装 uni 消息提示框
export const useToast = (options, isHide = false) => {
    const defaultOptions = {
        title: "操作成功",
        icon: "success", // success | loading | none
        duration: 1000,
        mask: false,
        position: "center", // top | bottom | center
    };

    const mergedOptions = { ...defaultOptions, ...options };

    return new Promise((resolve, reject) => {
        if (isHide) {
            uni.hideToast();
            return Promise.resolve(uni);
        }
        uni.showToast({
            ...mergedOptions,
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            },
        });
    });
};

// 封装 uni Loading显示框
export const useLoading = (options, isHide = false) => {
    const defaultOptions = {
        title: "努力加载中...",
        mask: false,
    };

    const mergedOptions = { ...defaultOptions, ...options };

    return new Promise((resolve, reject) => {
        if (isHide) {
            uni.hideLoading();
            return Promise.resolve(uni);
        }
        uni.showLoading({
            ...mergedOptions,
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            },
        });
    });
};

// 封装 uni 底部弹出操作菜单
export const showActionSheet = (options) => {
    const defaultOptions = {
        itemList: options.itemList,
    };

    const mergedOptions = { ...defaultOptions, ...options };

    return new Promise((resolve, reject) => {
        uni.showActionSheet({
            ...mergedOptions,
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            },
        });
    });
};

// 显示loading
export const showLoading = (text:string) => {
    uni.showLoading({
        title: text?text:"努力加载中...",
        mask: true
    });
};

// 隐藏loading
export const hideLoading = () => {
    uni.hideLoading();
};

// 显示成功提示框
export const messageSuccess = (msg:string, callback = null) => {
    uni.showToast({
        title: msg,
        icon: "success",
        mask: true,
        duration: 1000,
        success() {
            callback && callback();
        },
    });
};

// 显示错误提示框
export const messageError = (msg:string, callback = null) => {
    uni.showToast({
        title: msg,
        icon: "none",
        mask: true,
        duration: 1000,
        success() {
            callback && callback();
        },
    });
};
